//
//  ZLNetworkingTools.swift
//  新浪微博
//
//  Created by 朱玉龙 on 9/25/16.
//  Copyright © 2016 朱玉龙. All rights reserved.
//

import UIKit
import AFNetworking
enum HTTPMethod {
    case GET
    case POST
}

class ZLNetworkingTools: AFHTTPSessionManager {
    static let sharedTools:ZLNetworkingTools = {
        
        let tools = ZLNetworkingTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
      
        
        return tools
    }()
    

    func loadData(method:HTTPMethod, urlString:String, parameters:Any?, finishedBlock:@escaping (Any?,Error?) -> () ) {
        
        let successBlcok: (URLSessionDataTask, Any?) -> () = { (task:URLSessionDataTask, respondseObject:Any?) -> () in
            
            finishedBlock(respondseObject,nil)
        }
        
        let failueBlock = {(task:URLSessionDataTask?, error:Error) -> ()  in
            
            print(error)
            
            finishedBlock(nil,error)
            
        }

        
        if method == .GET {
            get(urlString, parameters: parameters, progress: nil, success: successBlcok, failure: failueBlock)
        } else {
            post(urlString, parameters: parameters, progress: nil, success: successBlcok, failure: failueBlock)
        }
        
    }
    
    
    
}
